package bb.lanxing.lib.devices.bryton.nordic.dfu;

import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class HexInputStream extends FilterInputStream {
    private final int LINE_LENGTH;
    private int available;
    private int bytesRead;
    private final byte[] localBuf;
    private int localPos;
    private int pos;
    private int size;

    private int asciiToInt(int i) {
        if (i >= 65) {
            return i - 55;
        }
        if (i < 48) {
            return -1;
        }
        return i - 48;
    }

    public HexInputStream(InputStream inputStream) throws HexFileValidationException, IOException {
        super(new BufferedInputStream(inputStream));
        this.LINE_LENGTH = 16;
        byte[] bArr = new byte[16];
        this.localBuf = bArr;
        this.localPos = 16;
        this.size = bArr.length;
        this.available = calculateBinSize();
    }

    private int calculateBinSize() throws IOException {
        InputStream inputStream = this.in;
        inputStream.mark(inputStream.available());
        try {
            int read = inputStream.read();
            int i = 0;
            while (true) {
                checkComma(read);
                int readByte = readByte(inputStream);
                inputStream.skip(4L);
                int readByte2 = readByte(inputStream);
                if (readByte2 == 0) {
                    i += readByte;
                } else if (readByte2 == 1) {
                    return i;
                }
                inputStream.skip((readByte * 2) + 2);
                while (true) {
                    read = inputStream.read();
                    if (read == 10 || read == 13) {
                    }
                }
            }
        } finally {
            inputStream.reset();
        }
    }

    @Override
    public int available() throws IOException {
        return this.available - this.bytesRead;
    }

    public int readPacket(byte[] bArr) throws HexFileValidationException, IOException {
        int i = 0;
        while (i < bArr.length) {
            int i2 = this.localPos;
            if (i2 < this.size) {
                byte[] bArr2 = this.localBuf;
                this.localPos = i2 + 1;
                bArr[i] = bArr2[i2];
                i++;
            } else {
                int i3 = this.bytesRead;
                int readLine = readLine();
                this.size = readLine;
                this.bytesRead = i3 + readLine;
                if (readLine == 0) {
                    break;
                }
            }
        }
        return i;
    }

    @Override
    public int read() throws IOException {
        throw new UnsupportedOperationException("Please, use readPacket() method instead");
    }

    @Override
    public int read(byte[] bArr) throws IOException {
        return readPacket(bArr);
    }

    @Override
    public int read(byte[] bArr, int i, int i2) throws IOException {
        throw new UnsupportedOperationException("Please, use readPacket() method instead");
    }

    public int sizeInBytes() throws IOException {
        return this.available;
    }

    public int sizeInPackets(int i) throws IOException {
        int sizeInBytes = sizeInBytes();
        return (sizeInBytes / i) + (sizeInBytes % i > 0 ? 1 : 0);
    }

    private int readLine() throws IOException {
        if (this.pos == -1) {
            return 0;
        }
        InputStream inputStream = this.in;
        while (true) {
            int read = inputStream.read();
            this.pos++;
            if (read != 10 && read != 13) {
                checkComma(read);
                int readByte = readByte(inputStream);
                int i = this.pos + 2;
                this.pos = i;
                this.pos = (int) (i + inputStream.skip(4L));
                int readByte2 = readByte(inputStream);
                int i2 = this.pos + 2;
                this.pos = i2;
                if (readByte2 != 0) {
                    if (readByte2 == 1) {
                        this.pos = -1;
                        return 0;
                    }
                    this.pos = (int) (i2 + inputStream.skip((readByte * 2) + 2));
                }
                if (readByte2 == 0) {
                    for (int i3 = 0; i3 < this.localBuf.length && i3 < readByte; i3++) {
                        int readByte3 = readByte(inputStream);
                        this.pos += 2;
                        this.localBuf[i3] = (byte) readByte3;
                    }
                    this.pos = (int) (this.pos + inputStream.skip(2L));
                    this.localPos = 0;
                    return readByte;
                }
            }
        }
    }

    @Override
    public synchronized void reset() throws IOException {
        super.reset();
        this.pos = 0;
        this.bytesRead = 0;
        this.localPos = 0;
    }

    private void checkComma(int i) throws HexFileValidationException {
        if (i == 58) {
            return;
        }
        throw new HexFileValidationException("Not a HEX file");
    }

    private int readByte(InputStream inputStream) throws IOException {
        return asciiToInt(inputStream.read()) | (asciiToInt(inputStream.read()) << 4);
    }
}
